#include "platform_config.h"
#include "myjni.h"

unsigned char GPRSLink_Flag=0;
char SMSReceiveBuffer[8*1024];
u32 RecodeCnter = 0;
extern u8 g_flagOfNewSMS;
char GPSData[200];
u8 GPRSRXBuf[512];
extern SysInf  SysInfor;
void ReceiveSMSPro()
{
    int i;
    int SMSNo;
    char *ptr;
    ptr = strchr(SMSReceiveBuffer,',');
    if (!ptr)
        return;
    ptr++;
    sscanf(ptr, "%d", &SMSNo);

    i = gsmReadMessageSingle(&MY_SM_PARAM,SMSNo);
    LOGE("$new message No:%d, i=%d",SMSNo, i);
    while(i > 0)
    {
        i--;
        memcpy(&MY_SM_PARAM,&MY_SM_PARAM_SMS[i],SM_PARAMLen);

        SMSPro();
    }
}

//读取SIM300主动上传的数据
int ReadSIM300TxData()
{
    int  ReceiveLengthTemp;
    char *ptr;
    unsigned int i=5;
    unsigned int ReceiveLength=0;
    if(strlen(SMSReceiveBuffer) == 0)return 0;
    if(strstr(SMSReceiveBuffer,"*HQ")!=NULL) //TCP数据
    {
        ptr=SMSReceiveBuffer;
        for(i=0;i<ReceiveLength;i++)
        {
            GPRSRXBuf[i]=*ptr++;
        }
        GPRSRXBuf[ReceiveLength]='\0';
        return 1;
    }
    else if(strstr(SMSReceiveBuffer,"RING")!=NULL)	 //电话呼入信息
    {
        return 2;
    }
    else if(strstr(SMSReceiveBuffer,"+CMTI:")!=NULL) //短消息信息
    {
        return 3;
    }
    else if(strstr(SMSReceiveBuffer,"CLOSED")!=NULL) //关闭连接
    {
        return 4;
    }
    else if(strstr(SMSReceiveBuffer,"NO CARRIER")!=NULL) //没有载波，对方切断电话
    {
        return 5	;
    }
    else if(strstr(SMSReceiveBuffer,"TCP ERROR")!=NULL) //TCP连接
    {
        return 6	;
    }
    else if(strstr(SMSReceiveBuffer,"ERROR")!=NULL) //ERROR，命令错误
    {
        return 7	;
    }
    else return 8;
}

void GPRSEncode(char *Encodebuf,GPRSStruct *GPRSStructp)
{
    char *ptr;
    ptr=(char *)Encodebuf;
    *ptr++='(';
    *ptr++='0';
    strcpy(ptr,(char *)SysInfor.terminalID);//流水号
    ptr+=strlen(ptr);
    memcpy(ptr,(char *)GPRSStructp->CMD,4);	  //命令字
    ptr+=4;
    strcpy(ptr,(char *)GPRSStructp->Message);//消息体
    ptr=(char *)Encodebuf;
    ptr+=strlen(ptr);
    *ptr++=')';
    *ptr='\0';
}

int GetGPSData(char *dest,char *gpsbuf)
{
    char *ptr;
    char *psr;
    unsigned char i;

    ptr=dest;
    psr=gpsbuf;
    if(strlen(psr)<10)return 0;
    for(i=0;i<9;i++)
    {
        psr=strchr(psr,',');  //查找日期
        psr++;
    }
    *ptr++=*(psr+4);
    *ptr++=*(psr+5);
    *ptr++=*(psr+2);
    *ptr++=*(psr+3);
    *ptr++=*(psr+0);
    *ptr++=*(psr+1);
    psr=gpsbuf;
    for(i=0;i<2;i++)
    {
        psr=strchr(psr,',');  //查找数据有效
        psr++;
    }
    *ptr++=*psr;
    psr=gpsbuf;
    for(i=0;i<3;i++)
    {
        psr=strchr(psr,',');  //维度
        psr++;
    }
    memcpy(ptr,psr,9);
    if(*ptr==',')
    {
        memset(ptr,'0',9);
        *(ptr+4)='.';
    }
    ptr+=9;
    psr=gpsbuf;
    for(i=0;i<4;i++)
    {
        psr=strchr(psr,',');  //N S
        psr++;
    }
    *ptr=*psr;
    if(*ptr==',')*ptr='N';
    ptr++;
    psr=gpsbuf;
    for(i=0;i<5;i++)
    {
        psr=strchr(psr,',');  //经度
        psr++;
    }
    memcpy(ptr,psr,10);
    if(*ptr==',')
    {
        memset(ptr,'0',10);
        *(ptr+5)='.';
    }
    ptr+=10;
    psr=gpsbuf;
    for(i=0;i<6;i++)
    {
        psr=strchr(psr,',');  //E W
        psr++;
    }
    *ptr=*psr;
    if(*ptr==',')*ptr='E';
    ptr++;
    ptr+=sprintf(ptr,"%05.1f",GPSInfor.NSpeed);//速度
    psr=gpsbuf;
    psr=strchr(psr,',');  //时间
    psr++;
    memcpy(ptr,psr,6);
    ptr+=6;

    psr=gpsbuf;
    for(i=0;i<9;i++)
    {
        psr=strchr(psr,',');  //方向
        psr++;
    }
    psr-=2;
    memset(ptr,'0',6);
    *(ptr+6)=0;
    ptr+=5;
    while(*psr!=',')*ptr--=*psr--;
    ptr=dest;
    ptr+=strlen(dest);
    *ptr++='1';
    *ptr++='1';
    *ptr++='1';
    *ptr++='1';
    *ptr++='1';
    *ptr++='1';
    *ptr++='1';
    *ptr++='1';
    *ptr='\0';
    return (ptr-dest);
}




/*------------------  天和协议 命令S23  ---------------------------------*/
void GPRSHQS23Encode(char *Sendbuf )
{
    char *ptr;
    char *psr;
    char i;
    unsigned int IOData=0xffffffff;
    ptr=Sendbuf;
    sprintf(ptr,"*HQ,");
    strcat(ptr,(char *)SysInfor.terminalID);
    strcat(ptr,",V4,S23,");
    ptr+=strlen(ptr);
    psr=SysInfor.IP;
    psr=strchr(psr,'"');
    psr++;
    while(*psr!='"')   //ip
    {
        *ptr++=*psr++;
    }
    *ptr++=':';
    psr=strchr(psr,',');
    psr+=2;
    while(*psr!='"')   //port
    {
        *ptr++=*psr++;
    }
    *ptr++=',';
    if(strlen(GPSData)>10)
    {
        psr=strstr(GPSData,"GPRMC");
        psr+=6;
        for(i=0;i<6;i++) //取得时间
        {
            *ptr++=*psr++;
        }
        *ptr++=',';
        psr=strstr(GPSData,"GPRMC");
        psr+=6;
        for(i=0;i<6;i++) //取得日期
        {
            *ptr++=*psr++;
        }
        psr=strchr(psr,',');
        strcpy(ptr,psr);
        ptr=Sendbuf;
        for(i=0;i<15;i++)
        {
            ptr=strchr(ptr,',');
            ptr++;
        }
      //  if(GPIO_ReadInputDataBit(ACC)==0)  //ACC得电
        {
            IOData &= 0xfffffeff;
        }
     //   if(GPIO_ReadInputDataBit(EXTPWR)==0)  //外部电瓶无电
        {
            IOData &= 0xfff7ffff;
        }
        ptr+=sprintf(ptr,"%X",IOData);
    }
    else
    {
      //  if(GPIO_ReadInputDataBit(ACC)==0)  //ACC得电
        {
            IOData &= 0xfffffeff;
        }
     //   if(GPIO_ReadInputDataBit(EXTPWR)==0)  //外部电瓶无电
        {
            IOData &= 0xfff7ffff;
        }
        ptr+=sprintf(ptr,"%X",IOData);
    }
    *ptr++='#';
    *ptr++='\r';
    *ptr++='\n';
    *ptr++=0;
}

void GPRSHQS32Encode(char *Sendbuf ,float distance)
{
    char *ptr;
    char *psr;
    char i;
    unsigned int IOData=0xffffffff;
    ptr=Sendbuf;
    sprintf(ptr,"*HQ,");
    strcat(ptr,(char *)SysInfor.terminalID);
    strcat(ptr,",V4,S32,");
    ptr+=strlen(ptr);
    ptr+=sprintf(ptr,"%.1f",distance);
    *ptr++=',';
    if(strlen(GPSData)>10)
    {
        psr=strstr(GPSData,"GPRMC");
        psr+=6;
        for(i=0;i<6;i++) //取得时间
        {
            *ptr++=*psr++;
        }
        *ptr++=',';
        psr=strstr(GPSData,"GPRMC");
        psr+=6;
        for(i=0;i<6;i++) //取得日期
        {
            *ptr++=*psr++;
        }
        psr=strchr(psr,',');
        strcpy(ptr,psr);
        ptr=Sendbuf;
        for(i=0;i<15;i++)
        {
            ptr=strchr(ptr,',');
            ptr++;
        }
     //   if(GPIO_ReadInputDataBit(ACC)==0)  //ACC得电
        {
            IOData &= 0xfffffeff;
        }
     //   if(GPIO_ReadInputDataBit(EXTPWR)==0)  //外部电瓶无电
        {
            IOData &= 0xfff7ffff;
        }
        ptr+=sprintf(ptr,"%X",IOData);
    }
    else
    {
        ptr+=sprintf(ptr,",,,,,,,,,");
     //   if(GPIO_ReadInputDataBit(ACC)==0)  //ACC得电
        {
            IOData &= 0xfffffeff;
        }
    //    if(GPIO_ReadInputDataBit(EXTPWR)==0)  //外部电瓶无电
        {
            IOData &= 0xfff7ffff;
        }
        ptr+=sprintf(ptr,"%X",IOData);
    }
    *ptr++='#';
    *ptr++='\r';
    *ptr++='\n';
    *ptr++=0;
}

void GPRSHQS17Encode(char *Sendbuf )
{
    char *ptr;
    char *psr;
    char i;
    unsigned int IOData=0xffffffff;
    ptr=Sendbuf;
    sprintf(ptr,"*HQ,");
    strcat(ptr,(char *)SysInfor.terminalID);
    strcat(ptr,",V4,S17,");
    ptr+=strlen(ptr);
    if(strlen(GPSData)>10)
    {
        psr=strstr(GPSData,"GPRMC");
        psr+=6;
        for(i=0;i<6;i++) //取得时间
        {
            *ptr++=*psr++;
        }
        *ptr++=',';
        psr=strstr(GPSData,"GPRMC");
        psr+=6;
        for(i=0;i<6;i++) //取得日期
        {
            *ptr++=*psr++;
        }
        psr=strchr(psr,',');
        strcpy(ptr,psr);
        ptr=Sendbuf;
        for(i=0;i<15;i++)
        {
            ptr=strchr(ptr,',');
            ptr++;
        }
      //  if(GPIO_ReadInputDataBit(ACC)==0)  //ACC得电
        {
            IOData &= 0xfffffeff;
        }
       // if(GPIO_ReadInputDataBit(EXTPWR)==0)  //外部电瓶无电
        {
            IOData &= 0xfff7ffff;
        }
        ptr+=sprintf(ptr,"%X",IOData);
    }
    else
    {
       // if(GPIO_ReadInputDataBit(ACC)==0)  //ACC得电
        {
            IOData &= 0xfffffeff;
        }
       // if(GPIO_ReadInputDataBit(EXTPWR)==0)  //外部电瓶无电
        {
            IOData &= 0xfff7ffff;
        }
        ptr+=sprintf(ptr,"%X",IOData);
    }
    *ptr++='#';
    *ptr++='\r';
    *ptr++='\n';
    *ptr++=0;
}


/*----------------- 天和标准格式 ------------------------------*/
void GPRSHQRecodeEncode(char *Sendbuf )
{
    //static char RecodeCnter=0;
    short iTemp = -1;
    static unsigned int Temp=0;
    char *ptr;
    char *psr;
    int i;
    char Strbuf[12];
    ptr=Sendbuf;
    memset(ptr,0,50);
    *ptr++='$';
    ptr+=gsmString2Bytes((unsigned char *)SysInfor.terminalID, (unsigned char *)ptr, 10);
    if(strlen(GPSData)>10)
    {
        psr=GPSData;
        for(i=0;i<1;i++)
        {
            psr=strchr(psr,',');  //查找时间
            psr++;
        }
        if(*psr!=',')ptr+=gsmString2Bytes((unsigned char *)psr,(unsigned char *)ptr, 6);
        else
        {
            memset(ptr,0,3);
            ptr+=3;
        }

        psr=GPSData;
        for(i=0;i<9;i++)
        {
            psr=strchr(psr,',');  //查找日期
            psr++;
        }
        if(*psr!=',')ptr+=gsmString2Bytes((unsigned char *)psr,(unsigned char *)ptr, 6);
        else
        {
            memset(ptr,0,3);
            ptr+=3;
        }
        psr=GPSData;
        for(i=0;i<3;i++)
        {
            psr=strchr(psr,',');  //查找纬度
            psr++;
        }
        if(*psr!=',')
        {
            ptr+=gsmString2Bytes((unsigned char *)psr,(unsigned char *)ptr, 4);
            psr+=5;
            ptr+=gsmString2Bytes((unsigned char *)psr,(unsigned char *)ptr, 4);
        }
        else
        {
            //memset(ptr,0,4);
            //ptr+=4;
            *ptr++=0x34;
            *ptr++=0x00;
            *ptr++=0x42;
            *ptr++=0x30;
        }

        *ptr++=0;

        psr=GPSData;
        for(i=0;i<5;i++)
        {
            psr=strchr(psr,',');  //查找经度
            psr++;
        }
        if(*psr!=',')
        {
            memcpy(Strbuf,psr,5);
            psr+=6;
            memcpy(&Strbuf[5],psr,4);
            ptr+=gsmString2Bytes((unsigned char *)Strbuf,(unsigned char *)ptr, 10);
        }
        else
        {
            //memset(ptr,0,5);
            //ptr+=5;
            *ptr++=0x11;
            *ptr++=0x34;
            *ptr++=0x80;
            *ptr++=0x83;
            *ptr++=0x1C;
        }
        ptr--;
        (*ptr)&=0xf0;
        psr=GPSData;
        for(i=0;i<6;i++)
        {
            psr=strchr(psr,',');  //E W
            psr++;
        }
        if(*psr=='E')(*ptr)|=0x08;
        else (*ptr)&=(~0x08);
        psr=GPSData;
        for(i=0;i<4;i++)
        {
            psr=strchr(psr,',');  //N S
            psr++;
        }
        if(*psr=='N')(*ptr)|=0x04;
        else (*ptr)&=(~0x04);
        psr=GPSData;
        for(i=0;i<2;i++)
        {
            psr=strchr(psr,',');  //A V
            psr++;
        }
        if(*psr=='A')(*ptr)|=0x02;
        else (*ptr)&=(~0x02);

        ptr++;

        psr=GPSData;
        for(i=0;i<7;i++)
        {
            psr=strchr(psr,',');  //速度
            psr++;
        }
        memset(Strbuf,'0',12);
        if(*psr!=',')
        {
            psr=strchr(psr,'.');  //速度
            if(psr!=NULL)
            {
                psr--;
                if(*psr!=',')Strbuf[2]=*psr;
                psr--;
                if(*psr!=',')
                {
                    Strbuf[1]=*psr;
                    psr--;
                    if(*psr!=',')Strbuf[0]=*psr;
                }
            }
            else
            {

            }
        }
        psr=GPSData;
        for(i=0;i<8;i++)
        {
            psr=strchr(psr,',');  //方向
            psr++;
        }
        if(*psr!=',')
        {
            psr=strchr(psr,'.');  //方向
            if(psr!=NULL)
            {
                psr--;
                if(*psr!=',')Strbuf[5]=*psr;
                psr--;
                if(*psr!=',')
                {
                    Strbuf[4]=*psr;
                    psr--;
                    if(*psr!=',')Strbuf[3]=*psr;
                }
            }
            else
            {
            }
        }

        ptr+=gsmString2Bytes((unsigned char *)Strbuf,(unsigned char *)ptr, 6);
        for(i=0;i<5;i++)*ptr++=0xff;
        *ptr++=0;
        *ptr++=RecodeCnter;
        RecodeCnter++;
//		iTemp=get_temp1820();
        if(iTemp!=0)
        {
            if(iTemp!=1360)Temp=iTemp;
        }
        ptr=Sendbuf;
        *(ptr+0x1e)=(unsigned char)(Temp&0x00ff);
        *(ptr+0x10)=(unsigned char)(Temp/256);
       // if(GPIO_ReadInputDataBit(ACC)==0)  //ACC得电
        {
            *(ptr+0x1b)&=0xfe;
        }
    }

}

